package cn.dnui_cr503.dnuifood_16110100503.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.ResultBean;
import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.model.UserModel;

/**
 * Created by chengrui on 2018/10/21.
 */

public class RegisterActivity extends BaseActivity implements OnRetrofitListener<ResultBean> {
    private EditText username,password,tele,address,comment,samepass;
    private Button reg;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViews();
        initEvents();
    }


    @Override
    public void onSuccess(ResultBean object, int flag) {
        if(object.getSuccess().equals("1")){
            Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
            startActivity(intent);
            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(RegisterActivity.this,"用户名密码已存在",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(RegisterActivity.this,"联网失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    void initViews() {
        layout_file= R.layout.activity_register;
        setLayout(layout_file);

        username=findViewById(R.id.reg_username);
        password=findViewById(R.id.reg_password);
        tele=findViewById(R.id.reg_tele);
        address=findViewById(R.id.reg_address);
        comment=findViewById(R.id.reg_comment);
        reg=findViewById(R.id.reg_reg);
        samepass=findViewById(R.id.user_samepass);
    }

    @Override
    void initEvents() {

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(samepass.getText().toString().equals(password.getText().toString())){
                    register();
                }else {
                    Toast.makeText(RegisterActivity.this,"请确认两次密码相同",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void register() {
        UserModel userModel=new UserModel();
        userModel.reg(username.getText().toString(),
                      password.getText().toString(),
                      tele.getText().toString(),
                      address.getText().toString(),
                      comment.getText().toString(),this);
    }

    @Override
    void initDate() {

    }
}
